
export default {
    data() {
    },
    methods: {
        /**
         * 通用图片上传检测
         * @param file 文件
         * @returns {boolean} 是否检测通过
         */
        beforeAvatarUpload(file) {
            let accept = ['image/jpeg', 'image/png', 'image/jpg'];
            const isJPG = accept.indexOf(file.type) > -1;
            const isLt2M = file.size / 1024 / 1024 < 5;

            if (!isJPG) {
                this.$message.error('上传图片只能是 JPG/PNG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传图片大小不能超过 5MB!');
            }
            return isJPG && isLt2M;
        },
        /**
         * 通过分类id获取子分类名字
         * @param subId 子分类id
         * @returns {string} 父类名字+子类名字
         */
        getSubName(subId) {
            let filter = this.$store.state.indexTypes.xiao_fenlei_data.filter(item => {
                return item.Id === subId;
            })[0];
            if (filter) {
                let brandName = this.getBrandName(filter.guishu_fenlei_id);
                return brandName + '/' + filter.fenlei_mingcheng;
            }
        },
        /**
         * 获取父类名字
         * @param id 父类id
         * @returns {string} 父类名称
         */
        getBrandName(id) {
            let brand = this.$store.state.indexTypes.da_fenlei_data.filter(item => {
                return item.Id === id;
            })[0];
            return brand ? brand.fenlei_mingcheng : '';
        },
        /**
         * 生成分类菜单树
         * @param brands 父级菜单数组
         * @param subBrands 子级菜单数组
         * @returns {Uint8Array | BigInt64Array | {img: *, children: [], label: *, value: *}[] | Float64Array | Int8Array | Float32Array | Int32Array | Uint32Array | Uint8ClampedArray | BigUint64Array | Int16Array | Uint16Array}
         */
        getBrandData(brands, subBrands) {
            return brands.map(brand => {
                let temp = {
                    label: brand.fenlei_mingcheng,
                    value: brand.Id,
                    img: brand.fenlei_tupian,
                    children: []
                };
                subBrands.forEach(sub => {
                    if (sub.guishu_fenlei_id === brand.Id) {
                        temp.children.push({
                            label: sub.fenlei_mingcheng,
                            value: sub.Id,
                            img: sub.fenlei_img
                        })
                    }
                });
                return temp
            })
        },
    },
    async created() {

    }
}